import os
import shutil
import configparser
import common.gyblog as log

# 读取配置
cf = configparser.ConfigParser()
cf.read(os.path.dirname(os.path.abspath('.')) + '/download_javbook/config.ini')
SERIES = cf.get('javbook', 'SERIES')



LOG = log.get_logger()
PATH = 'D:\workdir_2\\'
GOAL_PATH = 'D:\无遮挡机动车\\{}\\'.format(SERIES)

'''
按照年份分类:
前提：番号在最前面
'''


def varfiy_mp4s(path, goal_path):
    mp4s = os.listdir(path)
    for mp4 in mp4s:
        year = mp4[4:6]
        dir_path = goal_path + '20' + year
        if not os.path.exists(dir_path):
            os.mkdir(dir_path)
            LOG.info('创建文件夹成功：' + dir_path)
        old_path = path + mp4
        new_path = goal_path + '20' + year + '\\' + mp4
        try:
            if os.path.exists(new_path):
                print('文件已存在: {}'.format(new_path))
                LOG.info('文件已存在: {}'.format(new_path))
            else:
                shutil.move(old_path, new_path)
                print(old_path, new_path)
                LOG.info('移动视频：{},{}'.format(old_path, new_path))
        except FileExistsError as e:
            print(repr(e))
            LOG.error(repr(e))
            pass


if __name__ == '__main__':
    varfiy_mp4s(PATH, GOAL_PATH)
